let http = require('http')
let url_ = require('url')
let path = require('path')
let fs = require('fs')

let server = http.createServer()


server.on('request',(req,res)=>{
    let url = url_.parse(req.url).pathname;
    if (url === '/'){
        fs.readFile(path.join(__dirname,'static/index.html'),'utf-8',(err,data)=>{
            if(err){
                console.log(err);
            }else{
                // 读取静态资源文件响应给游览器
                res.setHeader('Content-type','text/html;charset=utf-8')
                res.end(data)
            }
        })
    }else if(url === '/product.html'){
        fs.readFile(path.join(__dirname,'static/product.html'),'utf-8',(err,data)=>{
            if(err){
                console.log(err); 
            }else{
                res.setHeader('Content-type','text/html;charset=utf-8')
                res.end(data)
            }
        })
    }else if(url === '/images/a.jpg'){
        fs.readFile(path.join(__dirname,'static/images/a.jpg'),(err,data)=>{
            if(err){
                console.log(err); 
            }else{
                res.setHeader('Content-type','image/jpeg')
                res.end(data)
            }
        })       
    }else if(url === '/images/b.jpg'){
        fs.readFile(path.join(__dirname,'static/images/b.jpg'),(err,data)=>{
            if(err){
                console.log(err); 
            }else{
                res.setHeader('Content-type','image/jpeg')
                res.end(data)
            }
        })       
    }else if(url === '/images/c.jpg'){
        fs.readFile(path.join(__dirname,'static/images/c.jpg'),(err,data)=>{
            if(err){
                console.log(err); 
            }else{
                res.setHeader('Content-type','image/jpeg')
                res.end(data)
            }
        })       
    }else if(url === '/css/main.css'){
        fs.readFile(path.join(__dirname,'static/css/main.css'),'utf-8',(err,data)=>{
            if(err){
                console.log(err); 
            }else{
                res.setHeader('Content-type','text/css;charset=utf-8')
                res.end(data)
            }
        })       
    }       
    else{
        fs.readFile(path.join(__dirname,'static/404.html'),'utf-8',(err,data)=>{
            if(err){
                console.log(err); 
            }else{
                res.setHeader('Content-type','text/html;charset=utf-8')
                res.end(data)
            }
        })
    }


})

server.listen(3002,()=>{
    console.log('is listening on 3002')
})




